<?php
// +----------------------------------------------------------------------
// | 大客户分析
// +----------------------------------------------------------------------
namespace app\admin\controller\v2;
use app\admin\Adminbase;
use base\files\Poster;
use app\admin\model\SystemParamValue;

class KeyAccount extends Adminbase
{
	
	/**
	* 获取大客户数据
	* /admin/v2/key_account/index
	* 需要登录 
	* @is_need_login false
	*/
	public function index($shop_id,$date=''){
		try {
			$x = model("admin/system_param_value")
				->where('param_code','buess_shop_khfx_dkh')
				->where('parent_value',$shop_id)
				->value('param_value') ?? '0~100,100~500,500';
				
			$data['x'] =  explode(",",$x);
			$data['y1'] = [ 'number'=>[],'money'=>[] ];
			$data['y2'] = [ 'number'=>[],'money'=>[] ];
			$data['maleTarget'] = ['pjnxf'=>[],'xfrs'=>[],'rszb'=>[],'xfje'=>[],'xfzb'=>[],'xfpjkdj'=>[]];
			$data['femaleTarget'] = ['pjnxf'=>[],'xfrs'=>[],'rszb'=>[],'xfje'=>[],'xfzb'=>[],'xfpjkdj'=>[]];
			
			foreach($data['x'] as $key => $value){
				$arr = explode("~",$value);
				
				$model = $this->getModel($shop_id,1,$date);
				$totalNum =  $model->count();
				$totalXf =  $model->sum('pay_price');
				if($arr[1]){
					$data['y1']['number'][] = $num = $model
						->where('b.pay_price','>=',$arr[0])
						->where('b.pay_price','<=',$arr[1])
						->count();
					$data['y1']['money'][] = $money = $model
						->where('b.pay_price','>=',$arr[0])
						->where('b.pay_price','<=',$arr[1])
						->sum('pay_price');
					//消费人数	
					$data['maleTarget']['xfrs'][] = $num;
					//人数占比
					$data['maleTarget']['rszb'][] = ($totalNum > 0 ? round($num / $totalNum * 100,2) : 0) .'%';
					//消费金额
					$data['maleTarget']['xfje'][] = $money;
					//消费占比
					$data['maleTarget']['xfzb'][] = ($totalXf > 0 ? round($money / $totalXf * 100,2) : 0) .'%';
					//消费平均客单价
					$data['maleTarget']['xfpjkdj'][] = ($num > 0 ? round($totalXf / $num ,2) : 0);
				}else{
					$data['y1']['number'][] =  $num = $model->where('b.pay_price','>',$arr[0])->count();
					$data['y1']['money'][] = $model
						->where('b.pay_price','>',$arr[0])
						->sum('pay_price');
					//消费人数	
					$data['maleTarget']['xfrs'][] = $num;
					//人数占比
					$data['maleTarget']['rszb'][] = ($totalNum > 0 ? round($num / $totalNum * 100,2) : 0) .'%';
					//消费金额
					$data['maleTarget']['xfje'][] = $money;
					//消费占比
					$data['maleTarget']['xfzb'][] = ($totalXf > 0 ? round($money / $totalXf * 100,2) : 0) .'%';
					//消费平均客单价
					$data['maleTarget']['xfpjkdj'][] =($num > 0 ? round($money / $num * 100,2) : 0);
				}
				
				$model = $this->getModel($shop_id,2,$date);
				$totalNum =  $this->getModel($shop_id,$sex)->count();
				$totalXf =  $this->getModel($shop_id,$sex)->sum('pay_price');
				if($arr[1]){
					$data['y2']['number'][] = $num = $model
						->where('b.pay_price','>=',$arr[0])
						->where('b.pay_price','<=',$arr[1])
						->count();
					$data['y2']['money'][] = $money = $model
						->where('b.pay_price','>=',$arr[0])
						->where('b.pay_price','<=',$arr[1])
						->sum('pay_price');
					//消费人数	
					$data['femaleTarget']['xfrs'][] = $num;
					//人数占比
					$data['femaleTarget']['rszb'][] = ($totalNum > 0 ? round($num / $totalNum * 100,2) : 0) .'%';
					//消费金额
					$data['femaleTarget']['xfje'][] = $money;
					//消费占比
					$data['femaleTarget']['xfzb'][] = ($totalXf > 0 ? round($money / $totalXf * 100,2) : 0) .'%';
					//消费平均客单价
					$data['femaleTarget']['xfpjkdj'][] = ($num > 0 ? round($totalXf / $num ,2) : 0);
				}else{
					$data['y2']['number'][] = $num = $model->where('b.pay_price','>',$arr[0])->count();
					$data['y2']['money'][] = $totalNum = $model
						->where('b.pay_price','>',$arr[0])
						->sum('pay_price');
					//消费人数	
					$data['femaleTarget']['xfrs'][] = $num;
					//人数占比
					$data['femaleTarget']['rszb'][] = ($totalNum > 0 ? round($num / $totalNum * 100,2) : 0) .'%';
					//消费金额
					$data['femaleTarget']['xfje'][] = $money;
					//消费占比
					$data['femaleTarget']['xfzb'][] = ($totalXf > 0 ? round($money / $totalXf * 100,2) : 0) .'%';
					//消费平均客单价
					$data['femaleTarget']['xfpjkdj'][] = ($num > 0 ? round($totalXf / $num ,2) : 0);
				}
				
				if(!$arr[1]) $data['x'][$key] = 	$value.'以上';	
				$data['maleTarget']['pjnxf'][] = $data['x'][$key];
				$data['femaleTarget']['pjnxf'][] = $data['x'][$key];
			}
			
			return $this->ReSucess($data);	
			
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 设置大客户
	* /admin/v2/key_account/set
	* 需要登录 
	* @is_need_login false	
	*/
	public function set($shop_id,$data){
		try {
			$res = model("admin/system_param_value")
				->where('param_code','buess_shop_khfx_dkh')
				->where('parent_value',$shop_id)
				->delete();
			$res = model("admin/system_param_value")->save([	
					'param_scope'=>'店铺参数',
					'param_code'=>'buess_shop_khfx_dkh',
					'parent_value'=>$shop_id,
					'param_value'=>$data,
				]);
			return $this->ReSucess(true);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 获取大客户数据
	* /admin/v2/analysis/getKeyAccountData
	* 需要登录 
	* @is_need_login false
	*/
	public function get($shop_id){
		try {
			$result = model("admin/system_param_value")
				->where('param_code','buess_shop_khfx_dkh')
				->where('parent_value',$shop_id)
				->value('param_value') ?? '';
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}

	//获取模型
	private function getModel($shop_id=-1,$sex=1,$dates=''){
		
		$sql = '1=1';
		if($dates){
			$sql =  'add_time>='.explode(" - ",$dates)[0];
			$sql =  $sql.' and add_time<='.explode(" - ",$dates)[1];
		}
		
		$model = model('user')->alias('a')
			->leftJoin("( 
				select uid,sum(pay_price) pay_price from xw_order 
				where is_pay = 1 and status >= 0 
				and $sql
				group by uid
			) b",'a.id = b.uid')
			->leftJoin("(
				select uid,add_time,count(*) nums from (
					select * from xw_shop_yy_order  where status = 1 
					order by id desc	
					limit 0,9999999999
				) a group by uid
			) c",'c.uid = a.id')
			->where('a.shop_id',$shop_id)
			->where(function($query) use($sex) {
				$query->where("a.sex",$sex);
				$query->whereor("a.sex", $sex == 1 ? '男' : '女' );
			});
			
		return $model;	
	}

}

